package location

import (
	"app/common/constant"
	"app/common/params"
	"app/common/response"
	"app/internal/codec"
	"app/internal/model"
	"app/pkg/log"
	"net/http"

	"github.com/gin-gonic/gin"
)

type LocationRequest struct {
	params.ParamsPagination
	State int8 `form:"state"`
}

// @Summary	地址列表
// @Schemes
// @Description
// @Tags		地址管理
// @Accept		mpfd
// @Param		Authorization	header		string											true	"签名"
// @Param		limit			query		int												true	"数量"
// @Param		page			query		int												true	"页码"
// @success	200				{object}	response.JSONResult{data=[]model.LocationModel}	"desc"
// @Router		/api/user/location/list [GET]
func List(c *gin.Context) {
	req := LocationRequest{}
	if err := codec.GinBind(c.Copy(), &req); err != nil {
		c.JSON(http.StatusOK, constant.ErrInfo{Code: constant.ParamsError, Msg: err.Error()})
		return
	}
	list, pagination, _ := model.NewLocationModel().List(c.GetInt64("uid"), req.Page, req.Limit)
	c.JSON(http.StatusOK, response.JSONPaginateResult{Paginate: pagination, Data: list})
}

// @Summary	我的收件地址
// @Schemes
// @Description
// @Tags		地址管理
// @Accept		mpfd
// @Param		Authorization	header		string											true	"签名"
// @success	200				{object}	response.JSONResult{data=model.LocationModel}	"desc"
// @Router		/api/user/location/info [GET]
func Info(c *gin.Context) {
	location, err := model.NewLocationModel().Info(c.GetInt64("uid"))
	if err != nil {
		c.JSON(http.StatusOK, constant.ErrInfo{Code: constant.LogicError, Msg: "未设置地址信息"})
		return
	}
	c.JSON(http.StatusOK, response.JSONResult{Data: location})
}

type ParamsEdit struct {
	Id       int64  `json:"id" validate:"required" message:"required:ID不能为空"` //ID
	Location string `json:"location" filter:"trim"`                           //地址
	Name     string `json:"name" filter:"trim"`
	Phone    string `json:"phone" validate:"cnMobile" label:"手机号" message:"cnMobile:手机号格式错误" filter:"trim"`
	Tag      string `json:"code" filter:"trim"` //标记
}

// @Summary	修改
// @Schemes
// @Description
// @Tags		地址管理
// @Accept		json
// @Param		payload	body		ParamsEdit			true	"body"
// @success	200		{object}	constant.ErrInfo	"desc"
// @Router		/api/user/location/edit [POST]
func Edit(c *gin.Context) {
	req := ParamsEdit{}
	if err := codec.GinBind(c, &req); err != nil {
		c.JSON(http.StatusOK, constant.ErrInfo{Code: constant.ParamsError, Msg: err.Error()})
		return
	}
	old, err := model.FindOne[model.LocationModel]("id=?", req.Id)
	if err != nil {
		log.NewError("location.Edit", err)
		c.JSON(http.StatusOK, constant.ErrInfo{Code: constant.ParamsError, Msg: "获取数据失败"})
		return
	}
	if req.Location != "" {
		old.Location = req.Location
	}
	if req.Tag != "" {
		old.Tag = req.Tag
	}

	if req.Name != "" {
		old.Name = req.Name
	}
	if req.Phone != "" {
		old.Phone = req.Phone
	}

	err = model.Save[model.LocationModel](&old)
	if err != nil {
		log.NewError("location.Edit", err)
		c.JSON(http.StatusOK, constant.ErrInfo{Code: constant.LogicError, Msg: "操作失败"})
		return
	}
	c.JSON(http.StatusOK, constant.OK)
}

type Addequest struct {
	Location string `json:"location" validate:"required" message:"required:地址不能为空" filter:"trim"`
	Name     string `json:"name" validate:"required" message:"required:姓名不能为空" filter:"trim"`
	Phone    string `json:"phone" validate:"required|cnMobile" label:"手机号" message:"required:手机号不能为空|cnMobile:手机号格式错误" filter:"trim"`
	Tag      string `json:"code" filter:"trim"`
}

// @Summary	添加
// @Schemes
// @Description
// @Tags		地址管理
// @Accept		json
// @Param		payload	body		Addequest			true	"body"
// @success	200		{object}	constant.ErrInfo	"desc"
// @Router		/api/user/location/add [POST]
func Add(c *gin.Context) {
	req := Addequest{}
	if err := codec.GinBind(c, &req); err != nil {
		c.JSON(http.StatusOK, constant.ErrInfo{Code: constant.ParamsError, Msg: err.Error()})
		return
	}

	err := model.Save[model.LocationModel](&model.LocationModel{
		UserId:   c.GetInt64("uid"),
		Location: req.Location,
		Phone:    req.Phone,
		Name:     req.Name,
		Tag:      req.Tag,
	})
	if err != nil {
		log.NewError("location.Add", err)
		c.JSON(http.StatusOK, constant.ErrInfo{Code: constant.LogicError, Msg: "操作失败"})
		return
	}
	c.JSON(http.StatusOK, constant.OK)
}

// @Summary	删除
// @Schemes
// @Description
// @Tags		地址管理
// @Accept		json
// @Param		Authorization	header		string					true	"签名"
// @Param		payload			body		params.ParamsMainKey	true	"body"
// @success	200				{object}	constant.ErrInfo		"desc"
// @Router		/api/user/location/del [post]
func Del(c *gin.Context) {
	req := params.ParamsMainKey{}
	if err := codec.GinBind(c.Copy(), &req); err != nil {
		c.JSON(http.StatusOK, constant.ErrInfo{Code: constant.ParamsError, Msg: err.Error()})
		return
	}
	_, err := model.FindOne[model.LocationModel]("id=?", req.Id)
	if err != nil {
		c.JSON(http.StatusOK, constant.ErrInfo{Code: constant.LogicError, Msg: "数据不存在"})
		return
	}
	if err := model.Delete[model.LocationModel](map[string]interface{}{"id": req.Id}); err != nil {
		log.NewError(c.GetString("requestId"), "LocationModel delete err", err.Error())
		c.JSON(http.StatusOK, constant.Failed)
		return
	}
	c.JSON(http.StatusOK, constant.OK)
}
